package cn.yx.common.security.webmvc.handler;

import cn.yx.common.core.enums.CharsetEnum;
import cn.yx.common.core.enums.ContentTypeEnum;
import cn.yx.common.core.pojo.ResponseVo;
import cn.yx.common.core.util.json.jackson.JsonUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>访问权限不足处理器（未授权处理器）</p>
 *
 * @author Wgssmart
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType(ContentTypeEnum.JSON.getValue());
        response.setCharacterEncoding(CharsetEnum.UTF8.getValue());
        response.getWriter().write(JsonUtil.toJsonStr(ResponseVo.authorizationError()));
    }
}
